package cc.zoyn.momentimageclubclient.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import cc.zoyn.momentimageclubclient.R;
import cc.zoyn.momentimageclubclient.builder.SnackbarBuilder;
import cc.zoyn.momentimageclubclient.builder.ToastBuilder;
import cc.zoyn.momentimageclubclient.fragment.MainFragment;
import cc.zoyn.momentimageclubclient.fragment.SchoolFragment;
import cc.zoyn.momentimageclubclient.util.NetworkUtils;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

public class MainActivity extends AppCompatActivity {

    private long exitTime;
    private boolean isOpen = false;

    //声明相关变量
    private Toolbar toolbar;
    private DrawerLayout mDrawerLayout;
    //Fragment
    private MainFragment mainFragment;
    private SchoolFragment schoolFragment;
    private BottomNavigationBar bottomNavigationBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews(); //获取控件
        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            // 不显示title
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            // 显示NavigationIcon
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        toolbar.setNavigationIcon(R.drawable.ic_menu_my); // 显示图标
        toolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });

        //创建返回键，并实现打开关/闭监听
        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                isOpen = true;
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                isOpen = false;
            }
        };
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        final Button button = (Button) findViewById(R.id.layout_drawer_relative_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!NetworkUtils.hasWebConnected()) {
                    ToastBuilder.createToast(MainActivity.this, Toast.LENGTH_SHORT)
                            .setMessage("未检测到网络连接!")
                            .setBackgroundColor(Color.WHITE)
                            .setMessageColor(Color.BLACK)
                            .show();
                }
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_menu_homepage, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.ic_menu_school, "学校"))
                .addItem(new BottomNavigationItem(R.drawable.ic_menu_take_photo, "拍照"))
                .addItem(new BottomNavigationItem(R.drawable.ic_menu_fix_picture, "修图"))
                .addItem(new BottomNavigationItem(R.drawable.ic_menu_lab, "实验室"))
                .setFirstSelectedPosition(0)
                // 点击方式
                .setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
                // 点击颜色
                .setActiveColor(R.color.blue_500)
                .setInActiveColor(R.color.grey_800)

                // 监听
                .setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(int position) {
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        switch (position) {
                            case 0:
                                if (mainFragment == null) {
                                    mainFragment = new MainFragment();
                                }
                                transaction.replace(R.id.id_content, mainFragment);
                                transaction.commit();
                                break;

                            case 1:
                                if (schoolFragment == null) {
                                    schoolFragment = new SchoolFragment();
                                }
                                transaction.replace(R.id.id_content, schoolFragment);
                                transaction.commit();
                                break;

                            case 2:
                                Intent intent = new Intent(Intent.ACTION_CAMERA_BUTTON);
                                startActivity(intent);
                                break;
                            case 3:
                                SnackbarBuilder.createSnackBar(toolbar)
                                        .setMessage("修!")
                                        .setDuration(Snackbar.LENGTH_SHORT)
                                        .show();
                                break;
                            case 4:
                                SnackbarBuilder.createSnackBar(toolbar)
                                        .setMessage("Test!")
                                        .setDuration(Snackbar.LENGTH_SHORT)
                                        .setAction("OK", new View.OnClickListener() {public void onClick(View view) {}})
                                        .show();
                                break;
                        }
                    }

                    @Override
                    public void onTabUnselected(int position) {
                    }

                    @Override
                    public void onTabReselected(int position) {
                    }
                })
                .initialise();

        // 初始化Fragment
        initialFragment();

    }

    private void initialFragment() {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        mainFragment = new MainFragment();
        schoolFragment = new SchoolFragment();
        transaction.add(mainFragment, "homepage").add(schoolFragment, "school");
        transaction.show(mainFragment).hide(schoolFragment).commit();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!isOpen) {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    Toast.makeText(this, "再按一次退出客户端", Toast.LENGTH_SHORT).show();
                    exitTime = System.currentTimeMillis();
                } else {
                    System.exit(0);
                }
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void findViews() {
        toolbar = (Toolbar) findViewById(R.id.main_toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.lay_left);
//        lvLeftMenu = (ListView) findViewById(R.id.lv_left_menu);
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.layout_bottom_bar);
    }
}
